﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _4._3.ASP.NET_Data_Binding
{
    public partial class MobileBg : System.Web.UI.Page
    {
        List<Producer> producersList;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.producersList = new List<Producer>();
                producersList.Add(new Producer() { ID = 1, Name = "Audi", Models = new List<string>() { "A4", "A3" } });
                producersList.Add(new Producer() { ID = 2, Name = "VW", Models = new List<string>() { "Faeton", "Passat", "Golf", "Polo", "Cudy", "Tuareg" } });
                producersList.Add(new Producer() { ID = 3, Name = "Alfa", Models = new List<string>() { "Romeo" } });
                producersList.Add(new Producer() { ID = 4, Name = "Fiat", Models = new List<string>() { "Punto", "Shit", "Mega shit" } });
                this.producers.DataSource = producersList;

                this.producers.DataTextField = "Name";
                this.producers.DataValueField = "ID";
                this.producers.DataBind();

                int selectedId = int.Parse(producers.SelectedValue);
                this.models.DataSource = producersList.FirstOrDefault(p => p.ID == selectedId).Models;
                this.models.DataBind();

                this.ViewState["producersList"] = producersList;

                List<Extra> extrasList = new List<Extra>() { new Extra(){Name= "ABS", HasExtra = false}
                    ,new Extra(){Name= "Hatchback", HasExtra=false},new Extra(){Name= "4doors", HasExtra=false},new Extra(){Name= "Winter tyres", HasExtra= false},
                    new Extra(){Name="Cleaners", HasExtra= false} };
                this.extras.DataSource = extrasList;
                this.extras.DataBind();

                List<string> engines = new List<string>() { "1.9TDI", "1.6" };
                this.engineProperties.DataSource = engines;
                this.engineProperties.DataBind();
               
            }
            else
            {
                this.producersList = (List<Producer>)this.ViewState["producersList"];
            }
        }

        protected void models_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void producers_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedId = int.Parse(producers.SelectedValue);
            this.models.DataSource = producersList.FirstOrDefault(p => p.ID == selectedId).Models;
            this.models.DataBind();
        }

        protected void submit_Click(object sender, EventArgs e)
        {
            string extrasString = string.Empty;

            foreach (DataListItem dlItem in extras.Items)
            {
                Label lbl = (Label)dlItem.FindControl("extraName");
                bool isChecked = ((CheckBox)dlItem.FindControl("hasExtra")).Checked;
                if (isChecked)
                {
                    extrasString += " " + lbl.Text;
                }
            }

            this.submitedInfo.Controls.Add(new LiteralControl("<p> Producer:" + this.producers.SelectedItem.Text + "</p>"));
            this.submitedInfo.Controls.Add(new LiteralControl("<p> Model:" + this.models.SelectedItem.Text + "</p>"));
            this.submitedInfo.Controls.Add(new LiteralControl("<p> Extras:" + extrasString + "</p>"));
            this.submitedInfo.Controls.Add(new LiteralControl("<p> Engine:" +this.engineProperties.SelectedItem.Text+ "</p>"));
        }

        
    }
}